package com.jwb.pub.serviceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.JavaModel;
import com.jwb.pub.model.Menu;
import com.jwb.pub.model.Page;
import com.jwb.pub.service.MenuService;
import com.jwb.util.ToolsUtil;


@Service
public class MenuServiceImpl implements MenuService
{
	// 权限DAO
	@Autowired
	private BaseDAO baseDAO;
	
	/**
	 * http://wj.tjjwwl.com/kpshow/dept/deptTreeEx.mvc?id=0
	 */
	public JSONObject getMenuTreeJson(JSONObject jsdata)
	{
		String sysName = jsdata.getString("sysName");
		int superior = jsdata.getInteger("superior");
		JSONObject jsobject = new JSONObject();
		try{
//			String sql = "select menu_id id, menu_superior pId, menu_name label, menu_grade level"+
//							 " from menu where sys_name='"+sysName+"' "+
//							 " start with menu_superior=-1 "+
//							 " connect by prior menu_id = menu_superior "+
//							 " order siblings by display_order ";			
//			jsobject= baseDAO.findBySqlToJosn(sql);
			jsobject.put("tree", queryTreeNodes(sysName,superior));
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "数据获取失败");
		}
		return jsobject;		
	}
	
	
	public JSONArray queryTreeNodes(String sysName, int superior) throws Exception{
			String sql = "select menu_id id, menu_superior pId, menu_name label, menu_grade level " +
					 " from menu where sys_name='"+sysName+"' and menu_superior="+superior+" order by display_order ";
			JSONArray result = new JSONArray();
			JSONObject jsobject = baseDAO.findBySqlToJosn(sql);
			if("0".equals(jsobject.getString("respCode"))){
				JSONArray list = jsobject.getJSONArray("list");
				if(list.size()>0){
					for(int i=0;i<list.size();i++){
						JSONObject obj = list.getJSONObject(i);
						int id = obj.getInteger("id");
						JSONArray children = queryTreeNodes(sysName,id);
						obj.put("children", children);
						result.add(obj);
					}
				}
			}			
		
		return result;
	}
	
	
	
	public JSONObject getMenuInfo(JSONObject jsdata){
		String menuId = jsdata.getString("menuId");
		JSONObject jsobject = new JSONObject();
		String sql = "select menu_name, menu_superior,(select menu_name from menu where menu_id= m.menu_superior) menu_superior_name, "
				+ "is_display, menu_href, display_order, menu_rights,( select remarks from privilege where priv_name=m.menu_rights) menu_rights_name, target, iconclass, menu_logo "+
				" from menu m where m.menu_id="+menuId;
		try {
			jsobject = baseDAO.findBySqlToJosn(sql);
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "数据获取失败");
		}		
		return jsobject;		
	}
	
	
	
	
	/**
	 * ：出菜单
	 */
	public JSONObject getSysMenu(JSONObject jsdata)
	{
		String sysName = jsdata.getString("sysName");
		String privNames = jsdata.getString("privNames");
		JSONObject jsobject = new JSONObject();
		StringBuffer sb = new StringBuffer();		
		String menuId1 = "";
		String menuId2 = "";
		try{
		
			List<Menu> list = getSysSuperMenu(sysName);
			
			for (int i = 0; i < list.size(); i++)
			{
				Menu m = list.get(i);
				
				String menuRights = m.getMenuRights() + ",";
				
				if (privNames.indexOf(menuRights) >= 0)
				{
					menuId1 = String.valueOf(m.getMenuId());
					
					sb.append("<li>");
					if (m.getMenuHref() == null||"".equals(m.getMenuHref()))
					{
						sb.append("<span class='list-group-item'>");
					}
					else
					{
						sb.append("<span class='list-group-item' onclick=\"window.parent.document.getElementById('mainFrame').src='"
								+ m.getMenuHref() + "'\">");
					}
					sb.append("<i class='"+m.getIconClass()+"'></i>");	
					sb.append(m.getMenuName());
					sb.append("<em class='glyphicon glyphicon-chevron-down'></em>");
					sb.append("</span>");
					
					if (this.hasChild(menuId1, sysName))
					{
						List<Menu> list1 = getSysChildMenu(menuId1, sysName);
						
						sb.append("<ul>");
						
						for (int j = 0; j < list1.size(); j++)
						{
							Menu n = list1.get(j);
							
							String menuRights2 = n.getMenuRights()
									+ ",";
							if (privNames.indexOf(menuRights2) >= 0)
							{
								menuId2 = String.valueOf(n.getMenuId());
								
								sb.append("<li>");
								if (n.getMenuHref() == null||"".equals(n.getMenuHref()))
								{
									sb.append("<a href='javascript:void(0)'>");
								}
								else
								{
									sb.append("<a href='javascript:void(0);' onclick=\"window.parent.document.getElementById('mainFrame').src='"
											+ n.getMenuHref() + "'\">");
								}
								sb.append("<i class='"+n.getIconClass()+"'></i>");
								sb.append(n.getMenuName());
								sb.append("</a>");
								
								if (this.hasChild(menuId2, sysName))
								{
									List<Menu> list2 = getSysChildMenu(menuId1,
											sysName);
									
									sb.append("<ul>");
									for (int k = 0; k < list2.size(); k++)
									{
										Menu o = list2.get(k);
										
										String menuRights3 =o.getMenuRights() + ",";
										if (privNames.indexOf(menuRights3) >= 0)
										{
											sb.append("<li>");
											if (o.getMenuHref() == null||"".equals(o.getMenuHref()))
											{
												sb.append("<a href='javascript:void(0)'>");
											}
											else
											{
												sb.append("<a href='javascript:void(0);' onclick=\"window.parent.document.getElementById('mainFrame').src='"
														+ o.getMenuHref() + "'\">");
											}
											sb.append(o.getMenuName());
											sb.append("</a>");
											sb.append("</li>");
										}
									}
									sb.append("</ul>");
								}
								sb.append("</li>");
							}
						}
						sb.append("</ul>");
					}
					sb.append("</li>");
				}
			}
			
			jsobject.put("menu", sb.toString());			
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "数据获取失败");
		}
				
		return jsobject;
	}

	
	public JSONObject getNewMenu(JSONObject jsdata){
		JSONObject jsobject = new JSONObject();
		String sysName = jsdata.getString("sysName");
		String privNames = jsdata.getString("privNames");
		try{
			JSONObject main = new JSONObject();
			String sql="select * from menu m where m.menu_superior=(select menu_id from menu where menu_superior=-1 and sys_name ='"+sysName+"') and m.is_display='Y'  and sys_name='"+sysName+"'"
					+ " order by m.display_order";
			main=baseDAO.findBySqlToJosn(sql);
		    if("0".equals(main.getString("respCode"))){
		    	JSONArray jsarry = new JSONArray();
		    	JSONArray list = main.getJSONArray("list");
		    	for(int i=0;i<list.size();i++){
		    		JSONObject jso = list.getJSONObject(i);
		    		if(privNames.indexOf(jso.getString("menu_rights")+",")>=0){
			    		String menuId = String.valueOf(jso.getInteger("menu_id"));
			    		if(this.hasChild(menuId, sysName)){
			    			JSONObject obj = new JSONObject();
			    			sql="select * from menu m where m.menu_superior="+menuId+" and m.is_display='Y'  and sys_name='"+sysName+"'"
									+ " order by m.display_order";
			    			obj=baseDAO.findBySqlToJosn(sql);
			    			if("0".equals(obj.getString("respCode"))){
			    				JSONArray childs = obj.getJSONArray("list");
			    				JSONArray result = new JSONArray();
			    				for(int j=0;j<childs.size();j++){
			    					JSONObject jobj = childs.getJSONObject(j);
			    					if(privNames.indexOf(jobj.getString("menu_rights")+",")>=0){
			    						result.add(jobj);
			    					}			    					
			    				}
			    				jso.put("children", result);
			    			}		    			
			    		}		    		
			    		jsarry.add(jso);
		    		}
		    	}
		    	
		    	jsobject.put("list", jsarry);
		    	jsobject.put("respCode", "0");
		    	jsobject.put("respDesc", "OK");
		    }else{
		    	return main;
		    }
		    
		}catch(Exception e){
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "获取菜单树失败");
		}
		
		return jsobject;
	}
	
	/**
	 * ：出菜单
	 */
	public JSONObject sysMenu(String sysName, String privNames){
		JSONObject jsobject = new JSONObject();
		StringBuffer sb = new StringBuffer();
		
		String menuId1 = "";
		String menuId2 = "";
		try{
			List<Menu> list = getSysSuperMenu(sysName);
			
			for (int i = 0; i < list.size(); i++)
			{
				Menu m = list.get(i);
				
				String menuRights = m.getMenuRights() + ",";
				
				if (privNames.indexOf(menuRights) >= 0)
				{
					menuId1 = String.valueOf(m.getMenuId());
					
					sb.append("<li>");
					if (m.getMenuHref() == null||"".equals(m.getMenuHref()))
					{
						sb.append("<span class='list-group-item'>");
						sb.append("<i class='"+m.getIconClass()+"'></i>");	
						sb.append(m.getMenuName());
						sb.append("<em class='glyphicon glyphicon-chevron-down'></em>");
						sb.append("</span>");
					}
					else
					{
						
						if("target=mainFrame".equals(m.getTarget()))
						{
							sb.append("<a style='cursor:pointer;' data-title='"+m.getMenuName()+"' data-addtab='"+m.getMenuName()+"' data-url='"+m.getMenuHref()+"' >");
							
						}
						else
						{
	//						sb.append("<a href='javascript:void(0);'  onclick=\" var tempwindow=window.open('_blank'); tempwindow.location.href='"
	//								+ m.getMenuHref() + "'\">");
							//sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+m.getMenuHref()+"','"+m.getMenuName()+"'); \">");
							sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+m.getMenuHref()+"','业务'); \">");
							
						}
						
						sb.append("<span class='list-group-item'>");
						sb.append("<i class='"+m.getIconClass()+"'></i>");	
						sb.append(m.getMenuName());
						sb.append("<em class='glyphicon glyphicon-chevron-down'></em>");
						sb.append("</span>");
						sb.append("</a>");
					}
					
					if (this.hasChild(menuId1, sysName))
					{
						List<Menu> list1 = getSysChildMenu(menuId1, sysName);
						
						sb.append("<ul>");
						
						for (int j = 0; j < list1.size(); j++)
						{
							Menu n = list1.get(j);
							
							String menuRights2 = n.getMenuRights()
									+ ",";
							if (privNames.indexOf(menuRights2) >= 0)
							{
								menuId2 = String.valueOf(n.getMenuId());
								
								sb.append("<li>");
								if (n.getMenuHref() == null||"".equals(n.getMenuHref()))
								{
									sb.append("<a href='javascript:void(0)'>");
								}
								else
								{
									
	//								sb.append("<a data-title='"+n.getMenuName()+"' data-addtab='"+n.getMenuName()+"' data-url='"+n.getMenuHref()+"' data-t='"+n.getTarget()+"' >");
							
									if("target=mainFrame".equals(n.getTarget()))
									{
										sb.append("<a style='cursor:pointer;' data-title='"+n.getMenuName()+"' data-addtab='"+n.getMenuName()+"' data-url='"+n.getMenuHref()+"' >");
										
									}
									else
									{
	//									sb.append("<a href='javascript:void(0);'  onclick=\" var tempwindow=window.open('_blank'); tempwindow.location.href='"
	//											+ n.getMenuHref() + "'\">");
										//sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+n.getMenuHref()+"','"+n.getMenuName()+"'); \">");
										sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+n.getMenuHref()+"','业务'); \">");
									}
								}
								
								sb.append("<i class='"+n.getIconClass()+"'></i>");
								sb.append(n.getMenuName());
								sb.append("</a>");
								
								if (this.hasChild(menuId2, sysName))
								{
									List<Menu> list2 = getSysChildMenu(menuId1,
											sysName);
									
									sb.append("<ul>");
									for (int k = 0; k < list2.size(); k++)
									{
										Menu o = list2.get(k);
										
										String menuRights3 =o.getMenuRights() + ",";
										if (privNames.indexOf(menuRights3) >= 0)
										{
											sb.append("<li>");
											if (o.getMenuHref() == null||"".equals(o.getMenuHref()))
											{
												sb.append("<a href='javascript:void(0)'>");
											}
											else
											{
	//											sb.append("<a data-title='"+o.getMenuName()+"' data-addtab='"+o.getMenuName()+"' data-url='"+o.getMenuHref()+"' data-t='"+o.getTarget()+"'>");
												if("target=mainFrame".equals(o.getTarget()))
												{
													sb.append("<a style='cursor:pointer;' data-title='"+o.getMenuName()+"' data-addtab='"+o.getMenuName()+"' data-url='"+o.getMenuHref()+"' >");
													
												}
												else
												{
	//												sb.append("<a href='javascript:void(0);'  onclick=\"var tempwindow=window.open('_blank'); tempwindow.location.href='"
	//														+ o.getMenuHref() + "'\">");
													//sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+o.getMenuHref()+"','"+o.getMenuName()+"'); \">");
													
													sb.append("<a href='javascript:void(0);'  onclick=\" window.open('"+o.getMenuHref()+"','业务'); \">");
												}
											
											
											}
											sb.append(o.getMenuName());
											sb.append("</a>");
											sb.append("</li>");
										}
									}
									sb.append("</ul>");
								}
								sb.append("</li>");
							}
						}
						sb.append("</ul>");
					}
					sb.append("</li>");
				}
			}
			
			if(sb.toString().equals("")){
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "获取Menu失败");
			}else{
				jsobject.put("ret", sb.toString());
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "获取Menu失败");
		}
		
		return jsobject;
	}
	
	
	public JSONObject btnInfo(JSONObject jsdata){
		String menuId = jsdata.getString("menuId");
		String menuLevel = jsdata.getString("menuLevel");
		JSONObject jsobject = new JSONObject();
		boolean addRight = true;
		boolean delRight = true;
		try {
			int level = Integer.valueOf(menuLevel);
			int limit = Integer.valueOf(ToolsUtil.getProp("config.properties", "menuLevel"));
			if(level>=limit){
				addRight=false;
			}
			String sql = "SELECT COUNT(*) FROM MENU WHERE MENU_SUPERIOR= "+menuId;
			int cnt = baseDAO.findBySqlCnt(sql);
			if(cnt>0 ||menuId.equals("11")||menuId.equals("43")) 
				delRight =false;
			jsobject.put("addRight", addRight);
			jsobject.put("delRight", delRight);
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		} catch (Exception e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "获取数据失败");
		}
		return jsobject;
	}
	
	
	public JSONObject delMenu(JSONObject jsdata){
		String menuId = jsdata.getString("menuId");
		JSONObject jsobject = new JSONObject();
		String sql = "SELECT menu_grade, menu_superior FROM MENU WHERE menu_id="+menuId;
		try {
			JSONObject jso = baseDAO.findBySqlToJosn(sql);
			JSONObject obj = jso.getJSONArray("list").getJSONObject(0);
			jsobject.put("menu_grade", obj.getInteger("menu_grade")-1);
			jsobject.put("menu_superior", obj.getInteger("menu_superior"));
			sql = "DELETE FROM MENU WHERE menu_id=?";
			ArrayList<JavaModel> list = new ArrayList<>();
			list.add(new JavaModel(Integer.valueOf(menuId)));
			if(baseDAO.excuteSql(sql,list)){
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}else{
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "删除失败");
			}	
		} catch (Exception e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "删除失败");
		}
			
		return jsobject;
	}
	
	
	public JSONObject getMenuRights(JSONObject jsdata){
		JSONObject jsobject = new JSONObject();
		JSONArray list = new JSONArray();
		String sql= "select distinct priv_group from privilege WHERE OPEN_STS='1'";
		try {
			JSONArray array = baseDAO.findBySqlToJosn(sql).getJSONArray("list");
			if(array.size()>0){
				for(int i=0;i<array.size();i++){
					JSONObject jso = new JSONObject();
					String group = array.getJSONObject(i).getString("priv_group");
					sql="select priv_name, remarks from privilege where priv_group='"+group+"' and open_sts='1'";
					jso.put("priv_group", group);
					JSONObject obj = baseDAO.findBySqlToJosn(sql);
					jso.put("privs", obj.getJSONArray("list"));
					list.add(jso);
				}		
				jsobject.put("rightList", list);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}else{
				jsobject.put("respCode", "9");
				jsobject.put("respDesc", "未找到数据");
			}
		} catch (Exception e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "查询数据失败");
		}		
		
		return jsobject;
	}
	
	/**
	 * 验证本层目录下是否存在子菜单
	 * @param menuId
	 * @param sysName
	 * @return
	 */
	private boolean hasChild(String menuId, String sysName)
	{
		try
		{
			String sql="select count(*) cnt from menu m where m.menu_superior="+menuId+" and m.is_display='Y'  and sys_name='"+sysName+"'";
			int sqlCnt=baseDAO.findBySqlCnt(sql);			
			
			return sqlCnt > 0 ? true : false;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	

	/**
	 * 获取父节点标识为0的菜单信息
	 * @param sysName
	 * @return
	 * @throws Exception 
	 */
	public List<Menu> getSysSuperMenu(String sysName) throws Exception
	{

		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
		List<Menu> list = new ArrayList<Menu>();
//			String sql="select * from menu m where m.menu_superior=0 and m.is_display='Y'  and sys_name='"+sysName+"'"
//						+ " order by m.display_order";
//			
			String sql="select * from menu m where m.menu_superior=(select menu_id from menu where menu_superior=-1 and sys_name ='"+sysName+"') and m.is_display='Y'  and sys_name='"+sysName+"'"
					+ " order by m.display_order";
		    jsobject=baseDAO.findBySqlToJosn(sql);
		    if(jsobject.getString("respCode").equals("0"))
		    {
		    	jsarry=jsobject.getJSONArray("list");
		    	for(int i=0;i<jsarry.size();i++)
		    	{
		    		Menu menu =new Menu();
		    		menu.setMenuId(jsarry.getJSONObject(i).getInteger("menu_id"));
		    		menu.setMenuRights(jsarry.getJSONObject(i).getString("menu_rights"));
		    		menu.setMenuName(jsarry.getJSONObject(i).getString("menu_name"));
		    		menu.setMenuGrade(jsarry.getJSONObject(i).getInteger("menu_grade"));
		    		menu.setMenuSuperior(jsarry.getJSONObject(i).getInteger("menu_superior"));
		    		menu.setIsDisplay(jsarry.getJSONObject(i).getString("is_display"));
		    		menu.setMenuHref(jsarry.getJSONObject(i).getString("menu_href"));
		    		menu.setDisplayOrder(jsarry.getJSONObject(i).getInteger("display_order"));
		    		menu.setSysName(jsarry.getJSONObject(i).getString("sys_name"));
		    		menu.setTarget(jsarry.getJSONObject(i).getString("target"));
		    		menu.setIconClass(jsarry.getJSONObject(i).getString("iconclass"));
		    		list.add(menu);
		    	}
		    }
		
		return list;
	
	}
	
	
	/**
	 * 获取子级别菜单信息
	 * @param menuId
	 * @param sysName
	 * @return
	 * @throws Exception 
	 */
	public List<Menu> getSysChildMenu(String menuId, String sysName) throws Exception
	{
		
		
		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
		List<Menu> list = new ArrayList<Menu>();
		String sql="select * from menu m where m.menu_superior="+menuId+" and m.is_display='Y'  and sys_name='"+sysName+"'"
								+ " order by m.display_order";
				jsobject=baseDAO.findBySqlToJosn(sql);
			    if(jsobject.getString("respCode").equals("0"))
				{
					jsarry=jsobject.getJSONArray("list");
					for(int i=0;i<jsarry.size();i++)
					{
					  Menu menu =new Menu();
					  menu.setMenuId(jsarry.getJSONObject(i).getInteger("menu_id"));
					  menu.setMenuRights(jsarry.getJSONObject(i).getString("menu_rights"));
					  menu.setMenuName(jsarry.getJSONObject(i).getString("menu_name"));
					  menu.setMenuGrade(jsarry.getJSONObject(i).getInteger("menu_grade"));
					  menu.setMenuSuperior(jsarry.getJSONObject(i).getInteger("menu_superior"));
					  menu.setIsDisplay(jsarry.getJSONObject(i).getString("is_display"));
					  menu.setMenuHref(jsarry.getJSONObject(i).getString("menu_href"));
					  menu.setDisplayOrder(jsarry.getJSONObject(i).getInteger("display_order"));
					  menu.setSysName(jsarry.getJSONObject(i).getString("sys_name"));
					  menu.setTarget(jsarry.getJSONObject(i).getString("target"));
					  menu.setIconClass(jsarry.getJSONObject(i).getString("iconclass"));
					  list.add(menu);
					}
				}
		return list;		    
	}
	
	
	public String havePrivName(String roleList)
	{
		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
		String privNames = "";
		String sql = "";
		try
		{
			
			sql = "select distinct p.priv_name priv_name from Privilege p, Role_Priv r "
					+ " where r.priv_code = p.priv_code and instr('" + roleList+"',"
					+" CONCAT(',',r.role_id,',' )) > 0 AND p.open_sts='1' ";
			
			 jsobject=baseDAO.findBySqlToJosn(sql);
			 if(jsobject.getString("respCode").equals("0"))
			    {
			    	jsarry=jsobject.getJSONArray("list");
			    	for(int i=0;i<jsarry.size();i++)
			    	{
			    	
			    		privNames = privNames + jsarry.getJSONObject(i).getString("priv_name") + ",";
			    		
			    	}
			    }
			
			return privNames;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}



	@Override
	public JSONObject getmenulist(JSONObject jsdata) {
		String sysName = jsdata.getString("sysName");
		JSONObject jsobject = new JSONObject();
        String sql = "";
        try
		{
			
			sql = "select m.* "+
				"  from menu m "+
				" where sys_name = '"+sysName+"'"+
				"   and is_display = 'Y'"+
			"	connect by prior menu_id = menu_superior"+
			"	 start with menu_superior = -1"+
			"	 order siblings by display_order ";
			jsobject=baseDAO.findBySqlToJosn(sql);
			 
		}
		catch (Exception e)
		{
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "查询数据异常");
		}
		return jsobject;
	}
	



	@Override
	public JSONObject getAppSysMenu(JSONObject jsobject,JSONObject jsdata) {
		String privNames = jsdata.getString("privNames");
		JSONObject jso = new JSONObject();
		if("0".equals(jsobject.getString("respCode")))
		{
			JSONArray  jsarry = new JSONArray();
			JSONArray  jsarry2 = new JSONArray();
			jsarry=jsobject.getJSONArray("list");
			for(int i=0;i<jsarry.size();i++)
			{
				JSONObject jso2 = new JSONObject();
				
				if(jsarry.getJSONObject(i).getInteger("menu_grade")>0)
				{
						
					String menuRights =jsarry.getJSONObject(i).getString("menu_rights") + ",";
				
					if (privNames.indexOf(menuRights) >= 0)
					{
					
					    jso2.put("menu_name",jsarry.getJSONObject(i).getString("menu_name") );
						jso2.put("menu_href",jsarry.getJSONObject(i).getString("menu_href") );
						jso2.put("menu_grade",jsarry.getJSONObject(i).getString("menu_grade") );			
						//jso2.put("target",jsarry.getJSONObject(i).getString("target") );
						//jso2.put("iconclass",jsarry.getJSONObject(i).getString("iconclass") );
						jso2.put("menu_logo",jsarry.getJSONObject(i).getString("menu_logo") );
						
						jsarry2.add(jso2);
					}
					
				}
			}
			
			jso.put("list", jsarry2);
			jso.put("respCode", jsobject.getString("respCode"));
			jso.put("respDesc", jsobject.getString("respDesc"));
		}
		else
		{
			jso.put("respCode", jsobject.getString("respCode"));
			jso.put("respDesc", jsobject.getString("respDesc"));
		}
		
		
		return jso;		
	}


	public JSONObject saveMenu(JSONObject jsdata){
		String menuId = jsdata.getString("menuId");
		String menuLevel = jsdata.getString("menuLevel");
		String sysName = jsdata.getString("sysName");
		String operType = jsdata.getString("operType");
		String jsonStr = jsdata.getString("jsonStr");
		JSONObject jsobject = new JSONObject();
		JSONObject info = JSONObject.parseObject(jsonStr);
		if(operType.equals("add")){
			try {
				int menuGrade = Integer.valueOf(menuLevel)+1;
				String sql = "insert into menu (menu_name, menu_grade, menu_superior, is_display, menu_href, display_order, menu_rights, sys_name, target, iconclass, menu_logo) " +
				" values (?,?,?,?,?,?,?,?,?,?,?) ";
				ArrayList<JavaModel> list = new ArrayList<>();
				Collections.addAll(list, new JavaModel(info.getString("name")),new JavaModel(menuGrade),new JavaModel(Integer.valueOf(menuId)),new JavaModel(info.getString("display")),new JavaModel(info.getString("url")), new JavaModel(info.getInteger("order")), 
						new JavaModel(info.getString("priv")), new JavaModel(sysName), new JavaModel(info.getString("target")),new JavaModel(info.getString("iconclass")),new JavaModel(info.getString("logo")));
				if(baseDAO.excuteSql(sql,list)){
					sql = "select menu_id from menu where menu_name='"+info.getString("name")+"' and menu_grade="+menuGrade+" and menu_superior="+menuId;
					JSONObject jso = baseDAO.findBySqlToJosn(sql).getJSONArray("list").getJSONObject(0);
					jsobject.put("menuId", jso.getInteger("menu_id"));
					jsobject.put("menuLevel", menuGrade);
					jsobject.put("respCode", "0");
					jsobject.put("respDesc", "OK");
				}else{
					jsobject.put("respCode", "-1");
					jsobject.put("respDesc", "添加失败");
				}				
			} catch (Exception e) {
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "添加失败");
			}			
			
		}else if(operType.equals("edit")){
			String sql = " update menu " +
						 " set menu_name = ?, "+
						 " is_display = ?," +
						 " menu_href = ? ," +
						 " display_order = ?, "+
						 " menu_rights = ? , "+
						 " target = ?, "+
						 " iconclass = ?, " +
						 " menu_logo = ? "+        
						 " where menu_id = ? and sys_name = ? ";			
			ArrayList<JavaModel> list = new ArrayList<>();
			Collections.addAll(list, new JavaModel(info.getString("name")),new JavaModel(info.getString("display")),new JavaModel(info.getString("url")),
					new JavaModel(info.getInteger("order")), new JavaModel(info.getString("priv")), new JavaModel(info.containsKey("target")?info.getString("target"):""),new JavaModel(info.containsKey("iconclass")?info.getString("iconclass"):""),
					new JavaModel(info.getString("logo")), new JavaModel(Integer.valueOf(menuId)), new JavaModel(sysName)
					);
			if(baseDAO.excuteSql(sql,list)){
				jsobject.put("menuId", menuId);
				jsobject.put("menuLevel", menuLevel);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}else{
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "保存失败");
			}	
			
		}		
		
		return jsobject;
	}


	public JSONObject sysMenus(Page p, JSONObject jsdata){
		JSONObject jsobject = new JSONObject();
		String sql = " SELECT (select menu_id from menu where menu_superior=-1 and sys_name = u.code and menu_name=u.code_desc) menu_id, code, code_desc,  decode((select count(*) from menu where menu_superior=-1 and sys_name = u.code and menu_name=u.code_desc),0,'0','1') exist, "+
					 " decode((select count(*) from menu m where m.sys_name = u.code and m.menu_superior <> -1), 0, 0, 1) is_leaf "+
					 " FROM USER_DICT u WHERE dict_name = 'sys_name' and STS='A' order by display_order ";
		try {
			jsobject = baseDAO.findBySqlToJosn(sql, p);
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "数据获取失败");
		}
		
		return jsobject;
	}

	public JSONObject addSysMenu(JSONObject jsdata){
		String code = jsdata.getString("code");
		String desc = jsdata.getString("desc");
		JSONObject jsobject = new JSONObject();
		try {
			String sql= " INSERT INTO MENU (MENU_NAME, MENU_GRADE, MENU_SUPERIOR, IS_DISPLAY, SYS_NAME) "+
						" VALUES (?,0,-1,'Y',?)";
			ArrayList<JavaModel> list = new ArrayList<>();
			list.add(new JavaModel(desc));
			list.add(new JavaModel(code));
			if(!baseDAO.excuteSql(sql,list)){
				jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "添加失败");
			}
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		} catch (Exception e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "添加失败");
		}		
		
		return jsobject;
	}
	
	public JSONObject delSysMenu(JSONObject jsdata){
		String menuId = jsdata.getString("menuId");
		JSONObject jsobject = new JSONObject();
		String sql = "delete from menu where menu_id = ?";
		ArrayList<JavaModel> list = new ArrayList<>();
		list.add(new JavaModel(Integer.valueOf(menuId)));
		if(!baseDAO.excuteSql(sql,list)){
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "删除失败");
			return jsobject;
		}
		jsobject.put("respCode", "0");
		jsobject.put("respDesc", "OK");
		
		return jsobject;
	}
	
}
	
